/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = test_jolt.c                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/01 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include "../../src/module/tuxedojolt/config.h"
#include "t_jolt_comm/t_jolt_comm.h"
#include "t_jolt_core/t_jolt_core.h"
#include "t_jolt_packet/t_jolt_packet.h"
#include "t_jolt_header/t_jolt_header.h"
#include "t_jolt_mar/t_jolt_mar.h"
#include "t_jolt_dataxfer/t_jolt_dataxfer.h"

#include <frservice_mng.h>

static Frmemstat_mng g_st_memstat_mng={0,FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t  g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

int main(int argc, char **argv)
{
    if (fr_slab_init(&g_st_mem_env, MODULE_NAME) == NULL)
        return FR_FAIL;

    apmtest_cunit_registry();

    FunType fun_suites[1024];
    memset(fun_suites, 0x00, sizeof(fun_suites));
    int i_suites_len = sizeof(fun_suites) / sizeof(FunType);
    int i_loop = 0;

    // 加载各个测试组件
    // t_jolt_comm
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_free);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_frtime2datetimestr);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_bin_to_hex_string);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_packet_is_in_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_report_msg_data_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_report_msg_data_resp);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_init_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_free_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_msg_data_node_free);

    // t_jolt_header
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_header_detect);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_header_unpack);

    // t_jolt_packet
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_init_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_pkt_is_fragmented);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_packet_check_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_packet_unfragment);

    // t_jolt_mar
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_mar_read_UTF4);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_mar_read_string);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_mar_read_mbstring);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_mar_detect_jolti18nflag);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_short);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_int);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_byte);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_boolean);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_long);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_float);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_double);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_string);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_bytes);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_mar_get_struct);

    // t_jolt_dataxfer
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_logon_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_trans_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_trans_resp);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_call_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_call_resp);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_dataxfer_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_dataxfer_resp);

    // t_jolt_core
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_private_session_init);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_analyze_direct);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_detect);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_destroy_session);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_destroy_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__jolt_destroy_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_session_destroy);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_jolt_analyze);

    for (;i_loop < i_suites_len; i_loop++)
    {
        if (fun_suites[i_loop] != NULL && fun_suites[i_loop]() != 0)
        {
            CU_cleanup_registry();
            return CU_get_error();
        }
    }

    apmtest_cunit_unregistry();

    fr_slab_destroy();

    return 0;
}
